package com.qst.express.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.commonUtils.result.R;
import com.qst.commonUtils.user.UserUtil;
import com.qst.express.entity.Courier;
import com.qst.express.entity.Express;
import com.qst.express.entity.User;
import com.qst.express.entity.vo.ExpressVo;
import com.qst.express.entity.vo.UserVo;
import com.qst.express.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mjh
 * @since 2022-09-25
 */
@Api(description = "用户管理")
@RestController
@RequestMapping("/e/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/console")
    public R console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> data = userService.console();
        System.out.println("111");
        return R.ok().data("userConsole", data);
   
    }
    /**
     * TODO 条件查询带分页
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "用户分页查询")
    @PostMapping("/pageUserCondition/{page}/{limit}")
    public R pageUserCondition(@PathVariable("page") Integer page,
                                  @PathVariable("limit") Integer limit,
                                  @RequestBody(required = false) UserVo userVo) {

        Page<User> userPage = new Page<>(page, limit);
        IPage<User> teacherIPage = userService.pageTeacherCondition(userPage, userVo);
        return R.ok().data("total", teacherIPage.getTotal()).data("rows", teacherIPage.getRecords());
    }
    /**
     * 添加快递员
     *
     * @return
     */
    @ApiOperation(value = "添加用户")
    @PostMapping("/addUser")
    public R addUser(@RequestBody(required = false) User user) {
        user.setCreatetime(new Date());
        boolean save = userService.save(user);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 删除快递员
     * @param userId
     * @return
     */
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/removeUser/{id}")
    public R removeUser(@PathVariable(value = "id") String userId) {
        boolean remove = userService.removeById(Integer.parseInt(userId));
        if (remove) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除用户")
    @PostMapping("/deletes")
    public R deltes(@RequestBody(required = false) Integer[] ids) {
        System.out.println(ids);
        boolean remove = false;
        for (Integer id : ids) {
            remove = userService.removeById(id);
        }
        if (remove) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    /**
     * 通过id查询快递员信息
     * @param userId
     * @return
     */
    @ApiOperation(value = "通过id查询用户信息")
    @GetMapping("/findUserById/{id}")
    public R findUserById(@PathVariable(value = "id") String userId) {
        User user = userService.getById(userId);
        if (user != null) {
            return R.ok().data("user", user);
        } else {
            return R.error();
        }
    }

    /**
     * 更新快递员信息
     * @param user
     * @return
     */
    @ApiOperation(value = "更新用户信息")
    @PutMapping("/updateUserById")
    public R updateCourierById(@RequestBody(required = false) User user){
        boolean update = userService.updateById(user);
        if (update) {
            return R.ok();
        } else {
            return R.error();
        }
    }



}

